/*
 * Created on Mar 3, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package littlecyberwar.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.filechooser.*;
import javax.swing.tree.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.event.*;
import java.io.*;
import java.lang.Runnable; 


import littlecyberwar.util.*;
import ModuleBuilder.control.SimpleFileFilter;
import ModuleBuilder.model.*;


/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class Loader {	
	static File file=null;
	static ScenarioModel scenarioMod=null;
	static JProgressBar progBar;
	static JLabel progText;
	static JFrame frame;
	static JFrame parent;
	static LoaderTask task ;
	static XMLLoaderTask XMLtask ;
	static Timer timer;
	public static String[] scenarios = new String[] {".scn"};
	static JFileChooser fc = new JFileChooser();
	
	public static void load(JFrame parentIn ) {
		parent = parentIn;

		//Show file chooser to allow the user to get the file
		fc.addChoosableFileFilter(new SimpleFileFilter(scenarios, "Scenario Builder Scenario files"));
		int returnVal = fc.showDialog(parent, "Select Scenario File Name To Load");
		file=null;
		
		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					createAndShowPrgressGUI(false);
				}
			});

		} else {
			Debug.out("Select Scenario File cancelled by user.");
		}
		return;
	}
	
	public static void createAndShowPrgressGUI(boolean isXML) {
		//Create and set up the window.
		
		frame = new JFrame();
		// frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		//Create and set up the content pane.
		Container newContentPane = frame.getContentPane();
//		newContentPane.setOpaque(true); //content panes must be opaque
//		frame.setContentPane(newContentPane);
		newContentPane.setLayout(new BorderLayout());
		progBar = new JProgressBar();
		progBar.setIndeterminate(true);
		progText = new JLabel("Loading Scenario...");
		newContentPane.add(progText, BorderLayout.CENTER);
		newContentPane.add(progBar, BorderLayout.PAGE_END);
		frame.pack();
		Dimension dlgSize = frame.getPreferredSize();
		Dimension frmSize = parent.getSize();
		Point loc = parent.getLocation();
		frame.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);

		//Display the window.

		frame.setVisible(true);
		frame.show();
		
		if ( isXML == false ) {
			doLoad();
		} else {
			doLoadXML();
		}
		

		return;
	}
		
	private static void doLoad() {
		task = new LoaderTask(fc, file, progText, frame, parent);
		
		//Create a timer.
		timer = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				progBar.setValue(task.getCurrent());
				String s = task.getMessage();
				if (task.isDone()) {
					Toolkit.getDefaultToolkit().beep();
					timer.stop();
//					startButton.setEnabled(true);
//					setCursor(null); //turn off the wait cursor
					progBar.setValue(progBar.getMinimum());
				}
			}
		});
		task.go();
		timer.start();
		return;
	}

	private static void doLoadXML() {
		XMLtask = new XMLLoaderTask(fc, file, progText, frame, parent);
		
		//Create a timer.
		timer = new Timer(1000, new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				progBar.setValue(task.getCurrent());
				String s = XMLtask.getMessage();
				if (XMLtask.isDone()) {
					Toolkit.getDefaultToolkit().beep();
					timer.stop();
//					startButton.setEnabled(true);
//					setCursor(null); //turn off the wait cursor
					progBar.setValue(progBar.getMinimum());
				}
			}
		});
		XMLtask.go();
		timer.start();
		return;
	}

	public static void loadXML(JFrame parentIn ) {
		parent = parentIn;

		//Show file chooser to allow the user to get the file
		fc.addChoosableFileFilter(new SimpleFileFilter(scenarios, "Scenario Builder Scenario XML files"));
		int returnVal = fc.showDialog(parent, "Select Scenario XML File Name To Load");
		file=null;
		
		//Process the results.
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			
			SwingUtilities.invokeLater(new Runnable() {
				public void run() {
					createAndShowPrgressGUI(true);
				}
			});

		} else {
			Debug.out("Select Scenario File cancelled by user.");
		}
		return;
	}
	

}
